package com.cims.common.utility;

import java.math.BigDecimal;
import java.text.DecimalFormat;


public class NumberUtils {
	public NumberUtils() {
	}

	public static double getRound(int decimalPlace, double dValue){
		double r = 0;

		BigDecimal bd = new BigDecimal(dValue);
		bd = bd.setScale(decimalPlace, BigDecimal.ROUND_UP);
		r = bd.doubleValue();
		
		return r;
	}

	public static int getIntFromString(String strValue) {
		if (strValue == null) return 0;
		
		strValue = StringUtils.trimAll(strValue);
		
		int iValue = 0;
		try {
			iValue = Integer.parseInt(strValue);
		} catch (NumberFormatException e) {
			iValue = 0;
		} catch (Exception ex){
			iValue = 0;
		}
		
		return iValue;
	}

	public static double getDoubleFromString(String strValue) {
		strValue = StringUtils.trimAll(strValue);
		
		double iValue = 0;
		try {
			iValue = Double.parseDouble(strValue);
		} catch (NumberFormatException e) {
			iValue = 0;
		}
		
		return iValue;
	}
	
	public static double getRoundUp(int decimalPlace, double dValue){
		double r = 0;

		BigDecimal bd = new BigDecimal(dValue);
		bd = bd.setScale(decimalPlace, BigDecimal.ROUND_UP);
		r = bd.doubleValue();
		
		return r;
	}
	
	public static double getRoundFloor(int decimalPlace, double dValue) {
		double r = 0;

		BigDecimal bd = new BigDecimal(dValue);
		bd = bd.setScale(decimalPlace, BigDecimal.ROUND_FLOOR);
		r = bd.doubleValue();
		
		return r;
	}
	
	public static String getFormatNumber(double dValue) {
		String pattern = "###,###.##";
		
		DecimalFormat myFormatter = new DecimalFormat(pattern);
		String output = myFormatter.format(dValue);
		
		if (output.indexOf(".") > 0) {
			output.substring(0, output.indexOf("."));
		}
		
		return output;
	}
	
	public static String getFormatNumber(int intValue) {
		String pattern = "###,###";
		
		DecimalFormat myFormatter = new DecimalFormat(pattern);
		String output = myFormatter.format(intValue);
		
		if (output.indexOf(".") > 0) {
			output.substring(0, output.indexOf("."));
		}
		
		return output;
	}
	
	public static double getNumberFromStr(String strValue) {
		double dblValue = 0;
		
		try {
			dblValue = Double.parseDouble(strValue);
		} catch (Exception e) {
			dblValue = 0;
		}
		
		return dblValue;
	}
	
	public static String getCurrencyFormatRoundUpNumber(double dValue) {
		String pattern = "###,###.###";
		double dValueRoundUp = getRoundUp(0, dValue);
		
		DecimalFormat myFormatter = new DecimalFormat(pattern);
		String output = myFormatter.format(dValueRoundUp);
		
		if (output.indexOf(".") > 0) {
			output.substring(0, output.indexOf("."));
		}
		
		return output;
	}
	
	public static void main(String[] args){
		double dbTest = 1.2715;
		System.out.println(getFormatNumber(dbTest));
	}
}

